package com.molichuxing.gateway.api.services.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.molichuxing.gateway.api.config.OptionalLogConfig;
import com.molichuxing.gateway.bff.goods.entity.response.*;
import com.molichuxing.services.property.StoreStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.GoodsApiService;
import com.molichuxing.gateway.bff.goods.entity.request.create.GoodsExclusiveCreateVo;
import com.molichuxing.gateway.bff.goods.entity.request.create.GoodsSubscribeCreateVo;
import com.molichuxing.gateway.bff.goods.services.GoodsAuthBffService;
import com.molichuxing.gateway.bff.goods.services.GoodsBffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 订单相关接口
 * 
 * @author zoumingyu
 * @date 2019年8月11日
 *
 */
@Api(tags = "商品相关接口")
@RestController
@RequestMapping(value = "/goods")
public class GoodsApiServiceImpl extends RootApiService implements GoodsApiService {
	private static final Logger logger = LoggerFactory.getLogger(GoodsApiServiceImpl.class);

	@Resource
	private GoodsBffService goodsBffService;

	@Resource
	private GoodsAuthBffService goodsAuthBffService;

	/**
	 * 获取商品列表信息
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取商品列表信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "goodsType", value = "商品类型", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "code", value = "商品编码", required = false, dataType = "String"),
			@ApiImplicitParam(name = "carStyleId", value = "车型id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/list")
	public ResponseResult<Paged<GoodsVo>> getGoodsList(@RequestParam Map<String, Object> params) {
		ResponseResult<Paged<GoodsVo>> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.getGoodsList(params));
		} catch (Exception e) {
			logger.error("[goodsList]获取商品列表信息异常", e);
			result.error("获取商品列表信息异常");
		}
		return result;
	}

	/**
	 * 获取商品基础信息
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取商品基础信息")
	@ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Integer")
	@GetMapping(value = "/base")
	public ResponseResult<GoodsBaseVo> getBaseGoods(@RequestParam Integer id) {
		ResponseResult<GoodsBaseVo> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.getBaseGoods(id));
		} catch (Exception e) {
			logger.error("[baseGoods]获取商品基础信息异常", e);
			result.error("获取商品基础信息异常");
		}
		return result;
	}

	/**
	 * 获取商品详细信息
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取商品详细信息")
	@ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Integer")
	@GetMapping(value = "/detail")
	public ResponseResult<GoodsDetailVo> getDetailGoods(@RequestParam Integer id) {
		ResponseResult<GoodsDetailVo> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.getDetailGoods(id));
		} catch (Exception e) {
			logger.error("[baseGoods]获取商品详细信息异常", e);
			result.error("获取商品详细信息异常");
		}
		return result;
	}

	/**
	 * 新增专属商品信息列表
	 * @param createDtoList
	 * @return
	 * @throws Exception
	 */
	@Override
	@ApiOperation(value = "新增专属商品信息列表")
	@PostMapping(value = "/exclusive/create")
	@OptionalLogConfig(methods="新增专属商品")
	public ResponseResult<Boolean> createExclusiveGoods(@RequestBody @Valid List<GoodsExclusiveCreateVo> createDtoList,
			BindingResult bindingResult) throws Exception {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.createExclusiveGoods(createDtoList));
		} catch (BizException e) {
			logger.error("[orderSubscribeFlow]新增专属商品信息列表异常:", e);
			result.error(e.getExceptionCode(), e.getMessage());
		} catch (Exception e) {
			logger.error("[baseGoods]新增专属商品信息异常", e);
			result.error("新增专属商品信息异常");
		}
		return result;
	}

	/**
	 * 新增专属商品信息列表
	 * @param createDtoList
	 * @return
	 * @throws Exception
	 */
	@Override
	@ApiOperation(value = "新增订阅商品信息列表")
	@PostMapping(value = "/subscribe/create")
	@OptionalLogConfig(methods="新增订阅商品")
	public ResponseResult<Boolean> createSubscribeGoods(@RequestBody List<GoodsSubscribeCreateVo> createDtoList) throws Exception {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.createSubscribeGoods(createDtoList));
		} catch (BizException e) {
			logger.error("[orderSubscribeFlow]新增订阅商品信息列表异常:", e);
			result.error(e.getExceptionCode(), e.getMessage());
		} catch (Exception e) {
			logger.error("[baseGoods]新增订阅商品信息异常", e);
			result.error("新增订阅商品信息异常");
		}
		return result;
	}

	/**
	 * 设置商品禁售状态
	 * @param id
	 * @param isForbidSale
	 * @return
	 * @throws Exception
	 */
	@Override
	@ApiOperation(value = "设置商品禁售状态")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isForbidSale", value = "是否禁售(0:否，1：是)", required = true, dataType = "Byte")

	})
	@GetMapping(value = "/setting/sale")
	@OptionalLogConfig(methods="设置商品禁售状态")
	public ResponseResult<Boolean> settingGoodsSale(@RequestParam int id, @RequestParam Byte isForbidSale) throws Exception {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.settingGoodsSale(id, isForbidSale));
		} catch (Exception e) {
			logger.error("[baseGoods]新增订阅商品信息异常", e);
			result.error("新增订阅商品信息异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "根据规格的key和商品类型查询规格信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "specList", value = "规格id集合", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "goodsType", value = "商品类型（1：专属，2：订阅）", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/spec")
	public ResponseResult<List<GoodsSpecConfigVo>> getGoodsProductSpecList(@RequestParam List<String> specList, @RequestParam int goodsType) throws Exception {
		ResponseResult<List<GoodsSpecConfigVo>> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.getGoodsProductSpecList(specList, goodsType));
		} catch (Exception e) {
			logger.error("[baseGoods]根据规格的key和商品类型查询规格信息异常", e);
			result.error("根据规格的key和商品类型查询规格信息异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "根据商品id获取分销商分页列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "dealerName", value = "经销商名称", required = false, dataType = "String"),
			@ApiImplicitParam(name = "cityId", value = "城市id", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "status", value = "分销状态(0:已开启，1：已关闭)", required = false, dataType = "Byte"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/dealer/page/list")
	public ResponseResult<Paged<GoodsDealerVo>> getDealerPage(@RequestParam(value="goodsId", required=true) Integer goodsId, @RequestParam(value="dealerName", required=false) String dealerName, @RequestParam(value="cityId", required=false) Integer cityId,
															  @RequestParam(value="status", required=false) Integer status, @RequestParam(value="pageNum", required=true) int pageNum, @RequestParam(value="pageSize", required=true) int pageSize) {
		ResponseResult<Paged<GoodsDealerVo>> result = new ResponseResult<>();
		try {
			return result.success(goodsAuthBffService.getDealerPage(goodsId, dealerName, cityId, status, pageNum, pageSize));
		} catch (Exception e) {
			logger.error("[baseGoods]根据商品id获取分销商分页列表异常", e);
			result.error("根据商品id获取分销商分页列表信息异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "根据商品id获取分销商列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "areaId", value = "区域id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "areaType", value = "区域类型（1：省，2：市）", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "status", value = "分销状态(0:已开启，1：已关闭)", required = false, dataType = "Byte")
	})
	@GetMapping(value = "/dealer/list")
	public ResponseResult<List<GoodsDealerVo>> getDealerList(@RequestParam Integer goodsId, @RequestParam Integer areaId, @RequestParam Integer areaType, @RequestParam(required = false) Integer status) {
		ResponseResult<List<GoodsDealerVo>> result = new ResponseResult<>();
		try {
			return result.success(goodsAuthBffService.getDealerList(goodsId, areaId, areaType, status));
		} catch (Exception e) {
			logger.error("[baseGoods]根据商品id获取分销商列表异常", e);
			result.error("根据商品id获取分销商列表信息异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "设置分销商的分销状态")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "dealerId", value = "经销商id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "status", value = "分销状态(0:已开启，1：已关闭)", required = true, dataType = "Byte")

	})
	@GetMapping(value = "/setting/distribution")
	@OptionalLogConfig(methods="设置商品分销商的分销状态")
	public ResponseResult<Boolean> settingGoodsDistribution(@RequestParam("goodsId") Integer goodsId, @RequestParam("dealerId") Integer dealerId,
															@RequestParam("status") Integer status) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			return result.success(goodsAuthBffService.settingGoodsDistribution(goodsId, dealerId, status));
		} catch (Exception e) {
			logger.error("[baseGoods]设置分销商的分销状态异常", e);
			result.error("设置分销商的分销状态异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "新增商品分销商")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "dealerIds", value = "经销商id集合", required = true, dataType = "List")

	})
	@GetMapping(value = "/create/dealer")
	@OptionalLogConfig(methods="新增商品分销商")
	public ResponseResult<Boolean> createGoodsDealer(@RequestParam("goodsId") Integer goodsId, @RequestParam("dealerIds") List<Integer> dealerIds) throws Exception {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			return result.success(goodsAuthBffService.createGoodsDealer(goodsId, dealerIds));
		} catch (Exception e) {
			logger.error("[baseGoods]新增商品分销商异常", e);
			result.error("新增商品分销商异常");
		}
		return result;
	}



	@Override
	@ApiOperation(value = "获取商品合同列表")
	@ApiImplicitParam(name = "businessType", value = "业务类型(1:订阅里程合同模板，2：专属里程合同模板，3：经销商采购合同模板)", required = true, dataType = "Integer")
	@GetMapping(value = "/contract/template")
	public ResponseResult<Paged<ContractTemplateVo>> getContractTemplateList(@RequestParam Integer businessType) {
		ResponseResult<Paged<ContractTemplateVo>> result = new ResponseResult<>();
		try {
			return result.success(goodsBffService.getContractTemplateList(businessType));
		} catch (Exception e) {
			logger.error("[baseGoods]获取商品合同列表异常", e);
			result.error("获取商品基础信息异常");
		}
		return result;
	}
}
